package domotica.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;

import domotica.client.EnvironmentThread;
import domotica.houseowner.HouseOwner;
import domotica.houseowner.Parameter;

@SuppressWarnings("serial")
public class ActuatorSensorGUI extends JPanel implements ActionListener{
	
	private static ActuatorSensorGUI actuatorSensorGUI;
	private static DomoticaGUI domoticaGUI;
	private static String actuatorSensorName;
	private static HouseOwner houseOwner;

	private static JScrollPane jScrollPane1;
	private static JButton jButton1;
	private static ArrayList<JSlider> jSliderList;
	private static SlidersJPanel jPanel2;
	private static JScrollPane jScrollPane2;
	private static JPanel jPanel3;
	private static JTable jTable1;
	private static JPanel jPanel1;
	
	private ActuatorSensorGUI(DomoticaGUI domoticaGUI, HouseOwner houseOwner, String actuatorSensorName){
		this.houseOwner=houseOwner;
		this.domoticaGUI=domoticaGUI;
		this.actuatorSensorName=actuatorSensorName;
		this.initGUI(actuatorSensorName);
	}
	
	public static ActuatorSensorGUI getActuatorSensorGUI(DomoticaGUI domoticaGUI, HouseOwner houseOwner, String actuatorSensorName){
		if (actuatorSensorGUI==null)
			actuatorSensorGUI=new ActuatorSensorGUI(domoticaGUI, houseOwner, actuatorSensorName);
		else
			actuatorSensorGUI.setSlidersPane(actuatorSensorName);
		
		return actuatorSensorGUI;
	}
	
	public static JPanel getActuatorSensorGUI(SlidersJPanel panel, String actuatorSensorName) {
		jPanel2=panel;
		jScrollPane1.setViewportView(jPanel2);
		actuatorSensorGUI.actuatorSensorName=actuatorSensorName;
		
		boolean isOn = houseOwner.isActuatorSensorOn(actuatorSensorName);
		jSliderList=panel.getJSliders();
		
		if (isOn){
			jButton1.setText("SPEGNI");
			for (JSlider slider : jSliderList)
				slider.setEnabled(false);
		} else {
			jButton1.setText("ACCENDI");
			for (JSlider slider : jSliderList)
				slider.setEnabled(true);
		}
		
		return actuatorSensorGUI;
	}

	private void setSlidersPane(String actuatorSensorName) {
		
		actuatorSensorGUI.actuatorSensorName=actuatorSensorName;
		
		jSliderList = new ArrayList<JSlider>();
		
		jPanel2 = new SlidersJPanel();
		BoxLayout jPanel2Layout = new BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2.setSize(250,250);
		jScrollPane1.setViewportView(jPanel2);
		
		ArrayList<Parameter> parameterList= new ArrayList<Parameter>(this.houseOwner.getActuatorSensorTaskDesiredParameterList(actuatorSensorName));
		
		JSlider slider;
		JLabel label;
		String parameterName;
		int min;
		int max;
	
		boolean isOn = this.houseOwner.isActuatorSensorOn(actuatorSensorName);
		
		if (isOn){
			jButton1.setText("SPEGNI");
		} else
			jButton1.setText("ACCENDI");
		
		for(Parameter parameter: parameterList){
			
			parameterName = parameter.getPropertyName();

			min = parameter.getMin();
			max = parameter.getMax();
			
			label = new JLabel(parameterName);
			label.setMaximumSize(new Dimension(240, 20));
			jPanel2.add(label);
			slider = new JSlider(min,max,(max+min)/2);
			slider.setEnabled(!isOn);
			jSliderList.add(slider);
			slider.setMajorTickSpacing(10);
			slider.setMinorTickSpacing(5);
			slider.setPaintTicks(true);
			slider.setPaintLabels(true);
			slider.setMaximumSize(new Dimension(550, 80));
			jPanel2.add(slider);
		}
		this.domoticaGUI.addActuatorSensorCacheElement(actuatorSensorName, jPanel2);
		jPanel2.setJSliders(this.jSliderList);
	}

	private void initGUI(String actuatorSensorName) {
		try {
			{
				GridLayout thisLayout = new GridLayout(1, 1);
				thisLayout.setColumns(1);
				thisLayout.setHgap(5);
				thisLayout.setVgap(5);
				this.setLayout(thisLayout);
				this.setSize(250, 250);
				this.setPreferredSize(new java.awt.Dimension(250, 250));
				{
					jScrollPane1 = new JScrollPane();
					this.add(jScrollPane1);
					{
						jSliderList = new ArrayList<JSlider>();
						jPanel2 = new SlidersJPanel();
						BoxLayout jPanel2Layout = new BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS);
						jPanel2.setLayout(jPanel2Layout);
						jPanel2.setSize(250,250);
						jScrollPane1.setViewportView(jPanel2);
						
						ArrayList<Parameter> parameterList= new ArrayList<Parameter>(houseOwner.getActuatorSensorTaskDesiredParameterList(actuatorSensorName));
							
						JSlider slider;
						JLabel label;
						String parameterName;
						int min;
						int max;
						
						boolean isOn = this.houseOwner.isActuatorSensorOn(actuatorSensorName);
										
						for(Parameter parameter: parameterList){
							
							parameterName = parameter.getPropertyName();

							min = parameter.getMin();
							max = parameter.getMax();
							
							label = new JLabel(parameterName);
							label.setMaximumSize(new Dimension(240, 20));
							jPanel2.add(label);
							slider = new JSlider(min,max,(max+min)/2);
							slider.setEnabled(!isOn);
							jSliderList.add(slider);
							slider.setMajorTickSpacing(10);
							slider.setMinorTickSpacing(5);
							slider.setPaintTicks(true);
							slider.setPaintLabels(true);
							slider.setMaximumSize(new Dimension(550, 80));
							jPanel2.add(slider);
						}
						this.domoticaGUI.addActuatorSensorCacheElement(actuatorSensorName, jPanel2);
						jPanel2.setJSliders(this.jSliderList);
					}
				}
				{
					jPanel1 = new JPanel();
					GridLayout jPanel1Layout = new GridLayout(2, 1);
					jPanel1Layout.setColumns(1);
					jPanel1Layout.setHgap(5);
					jPanel1Layout.setVgap(5);
					jPanel1Layout.setRows(2);
					jPanel1.setLayout(jPanel1Layout);
					{
						
					}
					this.add(jPanel1);
					{
						jPanel3 = new JPanel();
						jPanel1.add(jPanel3);
						GridBagLayout jPanel3Layout = new GridBagLayout();
						jPanel3Layout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
						jPanel3Layout.rowHeights = new int[] {7, 7, 7, 7};
						jPanel3Layout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
						jPanel3Layout.columnWidths = new int[] {7, 7, 7, 7};
						jPanel3.setLayout(jPanel3Layout);
						{
							jButton1 = new JButton();
							jButton1.setBackground(new Color(103,131,157));
							jPanel3.add(jButton1, new GridBagConstraints(1, 1, 2, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							boolean isOn = this.houseOwner.isActuatorSensorOn(actuatorSensorName);
							if (isOn){
								jButton1.setText("SPEGNI");
							} else
								jButton1.setText("ACCENDI");
							
							jButton1.addActionListener(this);
						}
					}
					{
						jScrollPane2 = new JScrollPane();
						jPanel1.add(jScrollPane2);
						{
							
							jTable1 = new JTable();
							jScrollPane2.setViewportView(jTable1);

							Thread t = new Thread(new EnvironmentThread(jTable1, this.houseOwner));
							
							t.start();

						}
					}
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent arg0) {
		
		try{
			if (jButton1.getText().equals("ACCENDI")){
				jButton1.setText("SPEGNI");
			
				ArrayList<String> parameters = new ArrayList<String>(); 
				
				for (JSlider slider : this.jSliderList){
					slider.setEnabled(false);
					parameters.add(String.valueOf(slider.getValue()));
			 	}
				
				this.houseOwner.addActuatorSensorTask(this.actuatorSensorName, parameters);
				
			} else {
				
				jButton1.setText("ACCENDI");
				for (JSlider slider : this.jSliderList)
					slider.setEnabled(true);
				
				houseOwner.stopTask(actuatorSensorName);
				
			}
		} catch (RuntimeException e){
			if (e.getMessage().equals("Component already on! Someone else may have added a task!")){
				JOptionPane.showMessageDialog(this,e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
			}
			if (e.getMessage().equals("Component already off! It may have completed its task or someone else could have turned it off!")){
				JOptionPane.showMessageDialog(this,e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
